package common;

/**
 * 客户端配置
 */
public class ClientConfig {
    // 代理服务器地址
    private String serverHost;

    // 代理服务器控制端口
    private int serverPort;

    // 是否启用身份验证
    private boolean authEnabled;

    // 身份验证令牌
    private String authToken;

    // 是否启用SSL
    private boolean sslEnabled;

    // 本地服务映射
    private ServiceMapping[] serviceMappings;

    // 连接重试次数上限
    private int maxRetries;

    // 连接超时时间 (毫秒)
    private int connectionTimeout;

    // 日志级别
    private String logLevel;

    // 日志文件路径
    private String logPath;

    // Getters and setters

    public String getServerHost() {
        return serverHost;
    }

    public void setServerHost(String serverHost) {
        this.serverHost = serverHost;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    public boolean isAuthEnabled() {
        return authEnabled;
    }

    public void setAuthEnabled(boolean authEnabled) {
        this.authEnabled = authEnabled;
    }

    public String getAuthToken() {
        return authToken;
    }

    public void setAuthToken(String authToken) {
        this.authToken = authToken;
    }

    public boolean isSslEnabled() {
        return sslEnabled;
    }

    public void setSslEnabled(boolean sslEnabled) {
        this.sslEnabled = sslEnabled;
    }

    public ServiceMapping[] getServiceMappings() {
        return serviceMappings;
    }

    public void setServiceMappings(ServiceMapping[] serviceMappings) {
        this.serviceMappings = serviceMappings;
    }

    public int getMaxRetries() {
        return maxRetries;
    }

    public void setMaxRetries(int maxRetries) {
        this.maxRetries = maxRetries;
    }

    public int getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(int connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public String getLogLevel() {
        return logLevel;
    }

    public void setLogLevel(String logLevel) {
        this.logLevel = logLevel;
    }

    public String getLogPath() {
        return logPath;
    }

    public void setLogPath(String logPath) {
        this.logPath = logPath;
    }

    /**
     * 从通用配置加载客户端配置
     */
    public static ClientConfig fromConfig(Config config) {
        ClientConfig clientConfig = new ClientConfig();

        clientConfig.setServerHost(config.getString("client.server.host", "localhost"));
        clientConfig.setServerPort(config.getInt("client.server.port", 7000));
        clientConfig.setAuthEnabled(config.getBoolean("client.auth.enabled", true));
        clientConfig.setAuthToken(config.getString("client.auth.token", "default_token"));
        clientConfig.setSslEnabled(config.getBoolean("client.ssl.enabled", false));
        clientConfig.setMaxRetries(config.getInt("client.max.retries", 10));
        clientConfig.setConnectionTimeout(config.getInt("client.connection.timeout", 30000));
        clientConfig.setLogLevel(config.getString("client.log.level", "INFO"));
        clientConfig.setLogPath(config.getString("client.log.path", "logs/client.log"));

        // 解析服务映射配置
        String mappingsStr = config.getString("client.service.mappings", "");
        String[] mappings = mappingsStr.split(",");

        ServiceMapping[] serviceMappings = new ServiceMapping[mappings.length];
        for (int i = 0; i < mappings.length; i++) {
            String mapping = mappings[i].trim();
            if (!mapping.isEmpty()) {
                String[] parts = mapping.split(":");
                if (parts.length >= 4) {
                    ServiceMapping serviceMapping = new ServiceMapping();
                    serviceMapping.setName(parts[0]);
                    serviceMapping.setRemotePort(Integer.parseInt(parts[1]));
                    serviceMapping.setLocalHost(parts[2]);
                    serviceMapping.setLocalPort(Integer.parseInt(parts[3]));

                    serviceMappings[i] = serviceMapping;
                }
            }
        }

        clientConfig.setServiceMappings(serviceMappings);

        return clientConfig;
    }
}
